using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using TappUp_Refresh;
using ImmunityBuster_wp7_2;


namespace ImmunityBuster._2D
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>

    public class Enemy2D 
    {
        #region Enemy Gen
        public interface IEnemy
        {
            void Draw(SpriteBatch spriteBatch);
            void Update(GameTimerEventArgs gameTime);
            Sprite getTex();
            void InfictDamage(int loss);
            float GetHealth();
            Vector2 GetPos();
            float GetRelativeDistance();
            void SetRelativeDistance(float dist);
            Vector2 getVelocity();
            void SetVelocity(Vector2 temp);
            Sprite GetEnemy();
            void SetEnemy(Sprite enemy);
            void SetRectangle(int rec);
            void SetPosition(Vector2 Position);
            Rectangle GetRectangle();
        }

        public class Enemy2 : IEnemy
        {
            //Animation a = new Animation();
            static Random rand = new Random();
            static float temp1 = rand.Next((new Random().Next(700, 1500)), 1600), temp2 = rand.Next(150, 390);
            static float Otemp11 = 0, Otemp22 = 0;
            public Sprite Enemy;
            public bool isClose = false;
            public float relDist = 0;
            float health = 100;
            public Sprite healthEnemy;
            ImmunityBuster_wp7_2.Levels level;
            public ContentManager content;
            public Enemy2(ContentManager Content, ImmunityBuster_wp7_2.Levels level)
            {
                this.content = Content;
                healthEnemy = new Sprite();
                healthEnemy.Texture = Content.Load<Texture2D>("HealthBar");
                Enemy = new Sprite();

                Enemy.Texture = Content.Load<Texture2D>("vfx\\WBCAnimation");
                Enemy.animation.Initialize(Content.Load<Texture2D>("vfx\\WBCAnimation"), new Vector2(), 150, 150, 8, 33, Color.White, 0.75f, true);

                while (temp1 == Otemp11)
                    temp1 = rand.Next((new Random().Next(700, 1500)), 3000);
                Enemy.Position.X = temp1;
                while (temp2 == Otemp22)
                {
                    temp2 = rand.Next(150, 390);
                }
                Enemy.Position.Y = temp2;
                Enemy.Velocity = new Vector2(-1, 0);
                Otemp11 = temp1;
                Otemp22 = temp2;
                this.level = level;

                healthEnemy.Bounds = new Rectangle((int)Enemy.Position.X, (int)Enemy.Position.Y - Enemy.Texture.Height / 4, 200, 10);
            }


            public float GetRelativeDistance()
            {
                return relDist;
            }

            public void SetRelativeDistance(float dist)
            {
                this.relDist = dist;
            }

            public Sprite GetEnemy()
            {
                return Enemy;
            }

            public void SetEnemy(Sprite enemy)
            {
                this.Enemy = enemy;
            }
            public Sprite getTex()
            {
                return Enemy;
            }

            public void InfictDamage(int loss)
            {
                health -= (int)loss;
            }


            public Vector2 getVelocity()
            {
                return Enemy.Velocity;
            }


            public void SetVelocity(Vector2 temp)
            {
                this.Enemy.Velocity = temp;
            }

            public void Draw(SpriteBatch spriteBatch)
            {
                //healthEnemy.Bounds = new Rectangle((int)Enemy.Position.X - Enemy.Texture.Bounds.Width / 2, (int)(Enemy.Position.Y - Enemy.Texture.Height / 3.5), 75, 10);
                healthEnemy.Bounds.Width = (int)(health * 0.75);
                //if(level != Levels.Level3)
                //spriteBatch.Draw(Enemy.Texture, Enemy.Position, Color.White);
                //else
                //    spriteBatch.Draw(Enemy.Texture, Enemy.Position, Color.Wheat);

                Enemy.animation.Draw(spriteBatch, Color.White);
                spriteBatch.Draw(healthEnemy.Texture, healthEnemy.Bounds, Color.White);
                //a.Draw(spriteBatch, Color.White);                
            }

            public void Update(GameTimerEventArgs gameTime)
            {
                healthEnemy.Bounds = Enemy.animation
                    .destinationRect;
                healthEnemy.Bounds.Y -= 10;
                healthEnemy.Bounds.Height = 10;
                Enemy.Position += Enemy.Velocity;
                Enemy.animation.Position = Enemy.Position;
                Enemy.animation.Update(gameTime);
                if (Enemy.Position.X < 0)
                    Enemy.Position.X = new Random().Next(700, 1200);

                if (Enemy.Position.Y > 400)
                    Enemy.Velocity.Y *= -1;

                if (Enemy.Position.Y < 100)
                    Enemy.Velocity.Y *= -1;
            }

            public float GetHealth()
            {
                return health;
            }


            public Vector2 GetPos()
            {
                return Enemy.animation.Position;
            }

            public void SetRectangle(int recWidth)
            {
                this.healthEnemy.Bounds.Width = recWidth;
            }

            public void SetPosition(Vector2 Position)
            {
                Enemy.Position = Position;
                
            }

            public Rectangle GetRectangle()
            {
                return new Rectangle((int)Enemy.Position.X, (int)Enemy.Position.Y, (int)Enemy.animation.FrameWidth, (int)Enemy.animation.FrameHeight);
            }
            /// <summary>
            /// Allows the game component to perform any initialization it needs to before starting
            /// to run.  This is where it can query for any required services and load content.
            /// </summary>
        }

        public List<IEnemy> gameSprites = new List<IEnemy>();
        List<Animation> animEnemies = new List<Animation>();
        #endregion
        int NoOfSprites = 0;
        public String Objective;
        public ImmunityBuster_wp7_2.Levels level = ImmunityBuster_wp7_2.Levels.Level1;

        SoundEffectInstance ExplosionMusic;
        ContentManager Content;

        public Enemy2D(ContentManager Content)            
        {
            this.Content = Content;
            ExplosionMusic = Content.Load<SoundEffect>("sounds\\explosion").CreateInstance();            
        }

        public void InitEnemies(ImmunityBuster_wp7_2.Levels level)
        {
            if (level == ImmunityBuster_wp7_2.Levels.Level1)
            {
                NoOfSprites = 14;
                Objective = "Attack the Helper-T Cells";
            }
            if (level == ImmunityBuster_wp7_2.Levels.Level2)
            {
                NoOfSprites = 22;
                Objective = "Final Objective for Level 1,\nAttack the Base System.\nDestroy the WBCs";
            }
            if (level == ImmunityBuster_wp7_2.Levels.Level3)
            {
                NoOfSprites = 3;
                Objective = "Final Objective for Level 1,\n Kill all the Macrophages \n& Monocytes";
            }

            this.level = level;

            for (int i = 0; i < NoOfSprites; i++)
            {
                Enemy2 ene = new Enemy2(Content, level);
                foreach (IEnemy enemyz in gameSprites)
                {
                    if (ene.GetPos() == enemyz.GetPos())
                        ene = new Enemy2(Content, level);
                }
                 
                gameSprites.Add(ene);
            }
            

        }

        public void Update(GameTimerEventArgs gameTime)
        {
            
            foreach (IEnemy enemy in gameSprites)
            {
                enemy.getTex().Bounds = new Rectangle((int)enemy.getTex().Position.X, (int)enemy.getTex().Position.Y, enemy.getTex().Texture.Bounds.Width, enemy.getTex().Texture.Height);
                enemy.Update(gameTime);
                enemy.SetRectangle((int)enemy.GetHealth() % 10);
            }


            try
            {
                foreach (IEnemy enemy in gameSprites)
                {
                    if (enemy.GetHealth() < 8)
                    {
                        Animation a = new Animation();
                        Vector2 temp = enemy.GetPos();
                        a.Initialize(Content.Load<Texture2D>("vfx\\WBC-Dead"), temp, 150, 150, 12, 50, Color.White, 0.75f, false);
                        animEnemies.Add(a);
                        gameSprites.Remove(enemy);
                    }
                }
            }
            catch (InvalidOperationException e)
            {
            
            }

            try
            {
                if (animEnemies.Count != 0)
                {
                    foreach (Animation a in animEnemies)
                    {
                        a.Update(gameTime);                        
                        if(ExplosionMusic.State != SoundState.Playing)
                             ExplosionMusic.Play();
                        if (!a.Active)
                        {
                            animEnemies.Remove(a);

                            (App.Current as App).currentScore.score += 20;
                        }
                    }
                }

                try
                {
                    for (int i = 0; i <= animEnemies.Count - 1; i++)
                    {
                        animEnemies[i].Position = gameSprites[i].GetPos();
                    }
                }
                catch (Exception e1)
                { }
            }
            catch (InvalidOperationException e)
            { 
            
            }
         
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (IEnemy enemy in gameSprites)
            {
                enemy.Draw(spriteBatch);
            }

            if (animEnemies.Count != 0)
            {
                foreach (Animation a in animEnemies)
                {
                    a.Draw(spriteBatch, Color.White);
                }
            }
                
        }

    }

    
}
